package com.fruit.service.rights.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fruit.mapper.maintenance.BaseOperationRecordMapper;
import com.fruit.service.rights.TraceBackManager;
import com.fruit.util.ResultInfo;
import com.fruit.util.ResultUtil;

import lombok.extern.slf4j.Slf4j;
/**
 * 追溯记录Service
 * @author Administrator
 * @version 2018.01.03 11:40
 */
@Slf4j
@Service("traceBackService")
public class TraceBackService implements TraceBackManager {
	@Autowired
	private BaseOperationRecordMapper baseOperationRecordMapper;
	@Override
	public Map<String, Object> getTbRecords(String tradeId, Map<String, Object> params) {
		log.info("【" + tradeId + "】---【追溯记录】---【开始】---");
		Map<String, Object> result = new HashMap<String, Object>();
		if (params == null) {
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参数不能为null!"));
			return result;
		}
		log.info("【" + tradeId + "】---【追溯记录】---【params】---{}", params.toString());
		if (null == params.get("id") || "".equals(params.get("id"))) {
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参数期次id不能为空!"));
			return result;
		}
		
		try {
			//获取追溯商品基本信息
			List<Map<String, Object>> baseInfo = baseOperationRecordMapper.getBaseInfo(params);
			log.info("【" + tradeId + "】---【追溯记录】---【追溯商品基本信息】---{}", baseInfo.toString());
			if (baseInfo == null || baseInfo.size() < 1) {
				throw new Exception("获取追溯商品基本信息异常！");
			}
			//获取追溯商品维护记录信息
			List<Map<String, Object>> records = baseOperationRecordMapper.getTbRecords(params);
			log.info("【" + tradeId + "】---【追溯记录】---【追溯商品维护记录信息】---{}", records.toString());
			result.put("baseInfo", baseInfo);
			result.put("dataList", records);
			result.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			log.error("【" + tradeId + "】---【追溯记录】---【异常】---{}", e);
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		log.info("【" + tradeId + "】---【追溯记录】---【结束】---");
		return result;
	}

}
